package com.hll.api.controller.xinhua;

import com.hll.api.config.web.RespMessage;
import com.hll.api.service.XinhuaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;


/**
 * 新华书店手机端接口
 * @author 何龙
 * @create 2019-07-09 22:03
 */
@RestController
@RequestMapping("/api/xinhua")
public class XinhuaController {
	@Resource
	XinhuaService service;


	/**
	 * @description 首页所有数据
	 */
	@GetMapping("/index")
	public RespMessage index(){
		return RespMessage.success(service.getIndex(),null);
	}

	/**
	 * @api {get} /api/xinhua/classification 页脚导航分类
	 * @apiVersion 1.0.0
	 * @apiName classification
	 * @apiGroup a_xinhua
	 */
	@GetMapping("/classification")
	public RespMessage classification(){
		return RespMessage.success(service.getClassification(),null);
	}

	/**
	 * @description 首页轮播图详情页面数据
	 */
	@GetMapping("/carousel/info")
	public RespMessage carouselInfo(String name){
		if (StringUtils.isEmpty(name)){
			return RespMessage.error("name 不能为空");
		}
		return RespMessage.success(service.getCarouselInfo(name),null);
	}


	/**
	 * @description 首页畅销排行详情页
	 */
	@GetMapping("/sellWell/ranking")
	public RespMessage sellWellRanking(){
		return RespMessage.success(service.getSellWellRanking(),null);
	}

	/**
	 * @description 首页专题详情页
	 */
	@GetMapping("/monographic")
	public RespMessage monographic(){
		return RespMessage.success(service.getMonographic(),null);
	}

	/**
	 * @description 首页优惠券详情页
	 */
	@GetMapping("/coupon")
	public RespMessage coupon(){
		return RespMessage.success(service.getCoupon(),null);
	}

	/**
	 * @title 综合接口
	 */
	@GetMapping("/activity/{path}")
	protected RespMessage activity(@PathVariable String path){
		return RespMessage.success(service.getActivity(path),null);
	}

	/** 图片反代 */
	@GetMapping("/img")
	public RespMessage getImg(String imgUrl, HttpServletResponse response) throws Exception {
		byte[] img=service.getImg(imgUrl);
		response.setContentType( "image/png" );
		OutputStream os = response.getOutputStream();
		os.write( img );
		os.flush();
		os.close();
		return null;
	}

	@GetMapping("/sms/num")
	public RespMessage smsNum(){
		return RespMessage.success(service.getSmsNum(),null);
	}
}
